//
//  ShareViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/3/22.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "ShareViewController.h"
#import "InformationModel.h"
#import "ShareItem.h"
#import "SandBoxHelper.h"
#import "HMScanner.h"
#import "ShareView.h"
#import "AppDelegate+UMengHelper.h"
@interface ShareViewController ()
@property (nonatomic,strong) UILabel *timeLab;
@property (nonatomic,strong) UILabel *titleLab;
@property (nonatomic,strong) UILabel *contentLab;
@property (nonatomic,strong) UIImageView *contentImageV;
@end

@implementation ShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self share];
}

- (void)setupUI{
    self.title = self.type == 1? XLocalized(@"快讯"): XLocalized(@"公告");
    UIImageView*topV = [UIImageView new];
    topV.image = IMAGE_NAMED( self.type == 1?@"shareTop": @"shareNoti") ;
    [self.contentView addSubview:topV];
    [topV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(AdaptedWidth(169));
    }];
    self.timeLab = [UILabel new];
    self.timeLab.textColor = UIColorHex(#ADADAD);
    self.timeLab.font = kDefaultFont(16);
    [self.contentView addSubview:self.timeLab];
    [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(AdaptedWidth(23));
        make.top.equalTo(topV.mas_bottom).offset(AdaptedWidth(30));
    }];
    self.titleLab = [UILabel new];
    self.titleLab.textColor = UIColorHex(#000000);
    self.titleLab.font = kBoldFont(16);
    self.titleLab.numberOfLines = 0;
    [self.contentView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(AdaptedWidth(23));
        make.top.equalTo(self.timeLab.mas_bottom).offset(AdaptedWidth(15));
        make.right.mas_equalTo(AdaptedWidth(-23));
    }];
    
    self.contentLab = [UILabel new];
    self.contentLab.textColor = UIColorHex(#666666);
    self.contentLab.font = kDefaultFont(16);
    self.contentLab.numberOfLines = 0;
    [self.contentView addSubview:self.contentLab];
    [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(AdaptedWidth(23));
        make.top.equalTo(self.titleLab.mas_bottom).offset(AdaptedWidth(20));
        make.right.mas_equalTo(AdaptedWidth(-23));
    }];
    if (self.shareM.cover.length) {
        self.contentImageV = [UIImageView new];
        self.contentImageV.contentMode= UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:self.contentImageV];
        [self.contentImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLab.mas_bottom).offset(AdaptedWidth(60));
            make.left.mas_equalTo(AdaptedWidth(23));
            make.right.mas_equalTo(AdaptedWidth(-23));
            make.height.mas_equalTo(1);
        }];
        [self.contentImageV sd_setImageWithURL:String_To_URL(self.shareM.cover) completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            // 等比例计算尺寸
            CGFloat pWidth = image.size.width < kScreenWidth-AdaptedWidth(26) ? image.size.width:kScreenWidth-AdaptedWidth(26);
            CGFloat pHeight = image.size.height * pWidth / image.size.width;
            [self.contentImageV mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(pHeight);
            }];
        }];
    }
    
    UIImageView*bottomV = [UIImageView new];
    bottomV.image = IMAGE_NAMED(@"shareBottom");
    [self.contentView addSubview:bottomV];
    [bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.shareM.cover.length?self.contentImageV.mas_bottom:self.contentLab.mas_bottom).offset(AdaptedWidth(60));
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(AdaptedWidth(117));
    }];
    self.timeLab.text = self.shareM.online_time;
    self.titleLab.text = self.shareM.title;
    self.contentLab.text = self.shareM.content;
    [self addNavigationItemWithTitles:@[XLocalized(@"分享")] isLeft:NO target:self action:@selector(rightBtnClick) tags:nil];
    
    UIView *codeBackV = [UIView new];
    codeBackV.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:codeBackV];
    [codeBackV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(AdaptedWidth(50));
        make.bottom.mas_equalTo(AdaptedWidth(-30));
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(100), AdaptedWidth(100)));
    }];
    codeBackV.layer.cornerRadius = AdaptedWidth(5);
    
    UIImageView*codeImgV = [UIImageView new];
    [codeBackV addSubview:codeImgV];
    [codeImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(AdaptedWidth(3));
        make.right.bottom.mas_equalTo(AdaptedWidth(-3));
    }];
   [HMScanner qrImageWithString:URL_App_Down avatar:IMAGE_NAMED([self GetAppIconName]) completion:^(UIImage *image) {
        codeImgV.image = image;
    }];
}
-(NSString*)GetAppIconName{
    NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
    NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
    return icon;
}
- (void)rightBtnClick{
    [self share];
}
//截图功能
-(UIImage *)captureImageFromView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.size, YES, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (void)share{
#pragma mark - 分享功能
//    NSString *docPath = [SandBoxHelper libCachePath];
//    NSString *imagePath = [docPath stringByAppendingString:@"/ShareImg.jpg"];
//    NSURL *shareobj = [NSURL fileURLWithPath:imagePath];
    UIImage * imagerang = [self captureImageFromView:self.contentView];
//    [imagerang.imageDataRepresentation writeToFile:imagePath atomically:YES];
//
//    ShareItem *item = [[ShareItem alloc] initWithData: imagerang andFile:shareobj];
    [ShareView showCallback:^(ShareType type) {
        UMSocialPlatformType umSocialPlatformType = type+1;
        [kAppDelegate shareImageToPlatformType:umSocialPlatformType shareImage:imagerang];
    }];
    return;
//    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[imagerang?:[UIImage new]] applicationActivities:nil];
//    //去除特定的分享功能
//    if (@available(iOS 9.0, *)) {
//        activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook,UIActivityTypePostToTwitter, UIActivityTypePostToWeibo,UIActivityTypeMessage,UIActivityTypeMail,UIActivityTypePrint,UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList,UIActivityTypePostToFlickr,UIActivityTypePostToVimeo,UIActivityTypePostToTencentWeibo,UIActivityTypeAirDrop,UIActivityTypeOpenInIBooks];
//            activityVC.completionWithItemsHandler = ^(NSString *activityType,BOOL completed,NSArray *returnedItems,NSError *activityError)
//            {
//            if (completed) { // 确定分享
//                DLog(@"分享");
//                }
//                else {
//                    DLog(@"取消");
//                }
//        };
//    } else {
//        activityVC.completionWithItemsHandler = ^(NSString *activityType,BOOL completed,NSArray *returnedItems,NSError *activityError)
//        {
//            if (completed) { // 确定分享
//                
//            }
//            else {
//                
//            }
//        };
//    }
//    [self presentViewController: activityVC animated:YES completion:nil];
}

@end
